package com.jc.oms.controller.task.orderToShop;

import com.jc.ds.model.JcTask;
import com.jc.ds.model.Xorder;
import com.jc.oms.controller.BaseController;
import com.jc.service.h5.ShopService;
import com.jc.service.jctask.JcTaskService;
import com.jc.service.order.XorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@Controller
@RequestMapping("orderToShop")
public class OrderToShopController extends BaseController {

    private Long ts = new Date().getTime();
    @Autowired
    private JcTaskService jcTaskService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private XorderService xorderService;


    @RequestMapping(value="/orderToShopTask" , method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> orderToShopTask(){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            JcTask jcTaskQu = new JcTask();
            jcTaskQu.setTaskType("push_order_shop");
            List<JcTask> jcTasks = jcTaskService.queryAll(jcTaskQu);
            for (JcTask jcTask:jcTasks) {
                //循环推送 推送次数<最大推送次数
                if (jcTask.getExeCount() < jcTask.getMaxExeCount()){
                    String orderId = jcTask.getTaskBusinessId();
                    Xorder xorder = xorderService.queryById(Long.valueOf(orderId));
                    shopService.pushOrder(xorder.getChannelOrderId());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }
}
